import React from 'react';
import {

} from 'react-native';

import BaseAction from '../common/BaseAction';
import Constants from '../../common/Constants';
import ResponseConstants from '../../common/ResponseConstants';

export default class CommonSendFetchAction extends BaseAction {
  constructor(props) {
    super(props);

    this.name = "CommonSendFetchAction";
  }

  finish(data, result, response) {
    if(result) {
      this._dealFinishSucess(data, response);
    }
    else {
      if(response.status === ResponseConstants.status_unauthorized) {
        this._dealUnauthorized(data.detail);
      }
      else {
        this._dealFinishFailed(data, response);
      }
    }
  }

  _dealFinishSucess(data, response) {
    let detail = "";
    if(data && data.detail != undefined) {
      detail = data.detail;
    }
    var updateParams = this.caller.state.updateParams;
    var newState = {
      viewStatus:response.status,
      isResponeUpdate:true,
      statusText:detail,
      updateAction:{name:this.name, data:this.data},
    };
    if(this.data.updateParamName) {
      if(updateParams === undefined || updateParams === null) {
        updateParams = {};
      }
      updateParams[this.data.updateParamName] = data;
      newState.updateParams = updateParams;
    }
    else {
      newState.data = data;
    }
    this.caller.setState(newState);
  }
}
